package test;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import cn.haona.game.simb.object.god.cache.CacheImpl_Memcached;
import cn.haona.game.simb.comm.util.IllWordFilterImplLocal;
import cn.haona.game.simb.comm.util.Util;
import cn.haona.game.simb.object.department.HAB;
import cn.haona.game.simb.object.god.CacheManager;
import cn.haona.game.simb.object.god.EntityManager;
import cn.haona.game.simb.object.god.MainManager;
import cn.haona.game.simb.object.god.PersistenceManager;

public class WorldLoader
{

	// WEB应用
	final public static String TAG_SERVERS = "servers";
	final public static String TAG_SERVER = "server";
	final public static String ATTR_SERVER_WEIGHTS = "weights";

	final public static String TAG_INIT_CONN_NUM = "initConnNum";
	final public static String TAG_MIN_CONN_NUM = "minConnNum";
	final public static String TAG_MAX_CONN_NUM = "maxConnNum";
	final public static String TAG_MAX_IDLE = "maxIdle";
	final public static String TAG_MAIN_T_SLEEP = "mainTsleep";

	final public static String TAG_SOCKET_TO = "socketTO";
	final public static String TAG_SOCKET_CONNECT_TO = "socketConnectTO";

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{

	}

	public void loadWorld()
	{
		initDB();
		initEntity();
		initCache();
	}

	private void initEntity()
	{

		// 关键字
		String illWordFileName = null;
		// 单位
		illWordFileName = "E:" + File.separator + "workspace" + File.separator + "simb" + File.separator
				+ "mod_simb_parent" + File.separator + "mod_simb_wap" + File.separator + "src" + File.separator
				+ "main" + File.separator + "webapp" + File.separator + "WEB-INF" + File.separator + "illword.txt";

		 //TCL笔记本
//		 illWordFileName="E:"+File.separator+"workspace"+File.separator+
//		 "maven"+File.separator+"simb"+File.separator+"mod_simb_wap"+File.separator+
//		 "src"+File.separator+"main"+File.separator+"webapp"+File.separator+"WEB-INF"+File.separator+"illword.txt";

		EntityManager em = new EntityManager();
		HAB hab = new HAB();
		em.setHab(hab);

		MainManager.getInstance().setEntityManager(em);
		IllWordFilterImplLocal ifl = new IllWordFilterImplLocal(illWordFileName);
		em.setIllWordFilterLocal(ifl);
	}

	// <!-- SIMB的MYSQL jdbc:mysql://x.x.x.x:3306/simb -->
	// <connectURI>jdbc:mysql://localhost:3306/simb</connectURI>
	// <!-- 用户名 -->
	// <username>root</username>
	// <!-- 密码 -->
	// <password>19840302</password>
	// <!-- 驱动字符串 -->
	// <driverClass>com.mysql.jdbc.Driver</driverClass>
	// <!-- 初始连接数 5 -->
	// <initialSize>5</initialSize>
	// <!-- 最大连接数 100 -->
	// <maxActive>100</maxActive>
	// <!-- 最大空闲连接数 30 -->
	// <maxIdle>30</maxIdle>
	// <!-- 连接池用完 客户端最大等待的数量 10000 -->
	// <maxWait>10000</maxWait>
	private void initDB()
	{
		String connectURI = "jdbc:mysql://localhost:3306/simb";
		String username = "root";
		String password = "19840302";
		String driverClass = "com.mysql.jdbc.Driver";
		String initialSize = "5";

		String maxActive = "100";
		String maxIdle = "30";
		String maxWait = "10000";

		PersistenceManager pManager = new PersistenceManager();
		pManager.setConnectURI(connectURI);
		pManager.setDriverClass(driverClass);
		pManager.setInitialSize(Integer.parseInt(initialSize));
		pManager.setMaxActive(Integer.parseInt(maxActive));
		pManager.setMaxIdle(Integer.parseInt(maxIdle));

		pManager.setMaxWait(Long.parseLong(maxWait));
		pManager.setPassword(password);
		pManager.setUsername(username);
		pManager.initDB();

		// 放入持久化管理器
		MainManager.getInstance().setPersistenceManager(pManager);
	}

	private void initCache()
	{
		String fileName = null;

		// 单位
		fileName = "E:" + File.separator + "workspace" + File.separator + "simb" + File.separator + "mod_simb_parent"
				+ File.separator + "mod_simb_wap" + File.separator + "src" + File.separator + "main" + File.separator
				+ "webapp" + File.separator + "WEB-INF" + File.separator + "init" + File.separator
				+ "cache_memcached.xml";

//		 //TCL笔记本
//		 fileName="E:"+File.separator+"workspace"+File.separator+
//		 "maven"+File.separator+"simb"+File.separator+"mod_simb_wap"+File.separator+
//		 "src"+File.separator+"main"+File.separator+"webapp"+File.separator+"WEB-INF"+ File.separator + "init" + File.separator
//			+ "cache_memcached.xml";

		// 字符串
		String strXML = Util.getStringFromLoadFile(fileName);

		// 解析数据
		Document document;
		try
		{
			document = DocumentHelper.parseText(strXML);
			// 跟元素
			Element root = document.getRootElement();

			String socketConnectTO = root.elementTextTrim(TAG_SOCKET_CONNECT_TO);
			String socketTO = root.elementTextTrim(TAG_SOCKET_TO);
			String mainTsleep = root.elementTextTrim(TAG_MAIN_T_SLEEP);
			String maxIdle = root.elementTextTrim(TAG_MAX_IDLE);
			String maxConnNum = root.elementTextTrim(TAG_MAX_CONN_NUM);

			String minConnNum = root.elementTextTrim(TAG_MIN_CONN_NUM);
			String initConnNum = root.elementTextTrim(TAG_INIT_CONN_NUM);

			// 服务器组
			Element elementServers = root.element(TAG_SERVERS);
			// 服务器
			List<Element> elementSs = elementServers.elements(TAG_SERVER);
			Iterator<Element> iteraElementSs = elementSs.iterator();

			// 远程缓存
			CacheImpl_Memcached remoteCache = new CacheImpl_Memcached();
			remoteCache.setInitConnNum(Integer.parseInt(initConnNum));
			remoteCache.setMainTsleep(Long.parseLong(mainTsleep));
			remoteCache.setMaxConnNum(Integer.parseInt(maxConnNum));
			remoteCache.setMaxIdle(Long.parseLong(maxIdle));
			remoteCache.setMinConnNum(Integer.parseInt(minConnNum));
			remoteCache.setSocketConnectTO(Integer.parseInt(socketConnectTO));
			remoteCache.setSocketTO(Integer.parseInt(socketTO));

			String[] arrServers = new String[elementSs.size()];
			Integer[] arrWeights = new Integer[elementSs.size()];
			for (int i = 0; i < arrServers.length; i++)
			{
				Element elementServer = elementSs.get(i);
				String ip = elementServer.getTextTrim();
				Attribute attr = elementServer.attribute(ATTR_SERVER_WEIGHTS);
				String weight = attr.getText();
				arrServers[i] = ip;
				arrWeights[i] = Integer.parseInt(weight);

			}
			remoteCache.setServers(arrServers);
			remoteCache.setWeights(arrWeights);

			remoteCache.initMe();

			// 缓存管理器
			CacheManager cacheManager = new CacheManager();
			cacheManager.setRemoteCache(remoteCache);
			// 缓存管理器放入总管理器中
			MainManager.getInstance().setCacheManager(cacheManager);

			// String key="4d59f0c8-cd7d-4dd5-b03c-16664eba6818";
			// remoteCache.add(key, "123");
			// String value=(String)remoteCache.get(key);
			//			
			// System.out.println(">>>>>>>>>>>>>>>remoteCache="+remoteCache);
			// WEB应用root
			// String webapp_root=root.elementTextTrim(TAG_WEBAPP_ROOT);
			// WebDefine.WEB_APP_ROOT=webapp_root;
			// System.out.println("web_app_root="+webapp_root);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

}
